package com.ptr.ptrsystem.servlet;

import com.ptr.controladores.conexionsqlserver;
import com.ptr.ptrsystem.dao.Lista;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author WALDIR
 */
@WebServlet(name = "BusquedaAniMatrimonioServlet", urlPatterns = {"/BusquedaAniMatrimonio"})
public class BusquedaAniMatrimonioServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String CodPersona = request.getParameter("CUI");
        String nroActaMatrimonial = request.getParameter("NAM");

        String nombre = request.getParameter("p_nom");
        String ape1 = request.getParameter("p_ape");
        String ape2 = request.getParameter("s_ape");

        String BTN = request.getParameter("btn1");

        if (BTN.equals("ActaMatrimonial")) {
            try {
                String query = "select * from TB_MATRIMONIO m,TB_UBIGEO ub,TB_NACIONALIDAD na where (m.IDUBIGEO=ub.idubigeo and na.COD_NACIONALIDAD=m.COD_NACIONALIDAD) and(m.NRO_ACTA_MAT='" + nroActaMatrimonial + "')";
                conexionsqlserver con = new conexionsqlserver();
                Connection cn = null;
                Statement st = null;
                ResultSet rs = null;
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(query);
                if (rs.next()) {
                    request.setAttribute("actaP", rs.getString("COD_PERSONA"));
                    request.setAttribute("actaM", rs.getString("NRO_ACTA_MAT"));
                    request.setAttribute("fecha", rs.getString("FECHA_REG_MAT"));
                    request.setAttribute("CDeU", rs.getString("id_ubigeo_dep"));
                    request.setAttribute("CPrU", rs.getString("id_ubigeo_prov"));
                    request.setAttribute("CDiU", rs.getString("id_ubigeo_dist"));
                    request.setAttribute("COtU", rs.getString("id_ubigeo_otro"));
                    request.setAttribute("NombreU", rs.getString("nom_ubigeo_dep") + " " + rs.getString("nom_ubigeo_prov") + " " + rs.getString("nom_ubigeo_dist") + " " + rs.getString("nom_ubigeo_otro"));
                    request.setAttribute("ubigeo", Lista.getNombreUbigeo(rs.getString("IDUBIGEO")));
                    request.setAttribute("P1", Lista.getNombrePersona(rs.getString("DNI_PERS_1")));
                    request.setAttribute("P2", Lista.getNombrePersona(rs.getString("DNI_PERS_2")));
                    request.setAttribute("T1", Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")));
                    request.setAttribute("T2", Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")));
                    request.setAttribute("Registrador", Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")));
                    request.setAttribute("estado", Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                    request.setAttribute("nacionalidad", Lista.getNombreNacionalidad(rs.getString("COD_NACIONALIDAD")));
                    request.setAttribute("ruta", rs.getString("RUTA_ACTA_MAT"));
                    request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
                }
                cn.close();
                st.close();
                rs.close();
                con.cerrarBD();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        if (BTN.equals("ActaPersonal")) {
            try {
                String query = "select * from TB_MATRIMONIO m,TB_UBIGEO ub,TB_NACIONALIDAD na where (m.IDUBIGEO=ub.idubigeo and na.COD_NACIONALIDAD=m.COD_NACIONALIDAD) and(m.COD_PERSONA='" + CodPersona + "')";
                conexionsqlserver con = new conexionsqlserver();
                Connection cn = null;
                Statement st = null;
                ResultSet rs = null;
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(query);
                if (rs.next()) {
                    request.setAttribute("actaP", rs.getString("COD_PERSONA"));
                    request.setAttribute("actaM", rs.getString("NRO_ACTA_MAT"));
                    request.setAttribute("fecha", rs.getString("FECHA_REG_MAT"));
                    request.setAttribute("CDeU", rs.getString("id_ubigeo_dep"));
                    request.setAttribute("CPrU", rs.getString("id_ubigeo_prov"));
                    request.setAttribute("CDiU", rs.getString("id_ubigeo_dist"));
                    request.setAttribute("COtU", rs.getString("id_ubigeo_otro"));
                    request.setAttribute("NombreU", rs.getString("nom_ubigeo_dep") + " " + rs.getString("nom_ubigeo_prov") + " " + rs.getString("nom_ubigeo_dist") + " " + rs.getString("nom_ubigeo_otro"));
                    request.setAttribute("ubigeo", Lista.getNombreUbigeo(rs.getString("IDUBIGEO")));
                    request.setAttribute("P1", Lista.getNombrePersona(rs.getString("DNI_PERS_1")));
                    request.setAttribute("P2", Lista.getNombrePersona(rs.getString("DNI_PERS_2")));
                    request.setAttribute("T1", Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")));
                    request.setAttribute("T2", Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")));
                    request.setAttribute("Registrador", Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")));
                    request.setAttribute("estado", Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                    request.setAttribute("nacionalidad", Lista.getNombreNacionalidad(rs.getString("COD_NACIONALIDAD")));
                    request.setAttribute("ruta", rs.getString("RUTA_ACTA_MAT"));
                    request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
                }
                cn.close();
                st.close();
                rs.close();
                con.cerrarBD();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (BTN.equals("nombres")) {
            try {
                String query = "select * from TB_MATRIMONIO m,TB_UBIGEO ub,TB_NACIONALIDAD na,TB_PERSONA per where (m.IDUBIGEO=ub.idubigeo and na.COD_NACIONALIDAD=m.COD_NACIONALIDAD and per.COD_PERSONA=m.COD_PERSONA) and(per.PRE_NOM_PERS='" + nombre.toUpperCase() + "' and per.PRIM_APE_PERS='" + ape1.toUpperCase() + "' and per.SEG_APE_PERS='" + ape2.toUpperCase() + "')";
                conexionsqlserver con = new conexionsqlserver();
                Connection cn = null;
                Statement st = null;
                ResultSet rs = null;
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(query);
                if (rs.next()) {
                    request.setAttribute("actaP", rs.getString("COD_PERSONA"));
                    request.setAttribute("actaM", rs.getString("NRO_ACTA_MAT"));
                    request.setAttribute("fecha", rs.getString("FECHA_REG_MAT"));
                    request.setAttribute("CDeU", rs.getString("id_ubigeo_dep"));
                    request.setAttribute("CPrU", rs.getString("id_ubigeo_prov"));
                    request.setAttribute("CDiU", rs.getString("id_ubigeo_dist"));
                    request.setAttribute("COtU", rs.getString("id_ubigeo_otro"));
                    request.setAttribute("NombreU", rs.getString("nom_ubigeo_dep") + " " + rs.getString("nom_ubigeo_prov") + " " + rs.getString("nom_ubigeo_dist") + " " + rs.getString("nom_ubigeo_otro"));
                    request.setAttribute("ubigeo", Lista.getNombreUbigeo(rs.getString("IDUBIGEO")));
                    request.setAttribute("P1", Lista.getNombrePersona(rs.getString("DNI_PERS_1")));
                    request.setAttribute("P2", Lista.getNombrePersona(rs.getString("DNI_PERS_2")));
                    request.setAttribute("T1", Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")));
                    request.setAttribute("T2", Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")));
                    request.setAttribute("Registrador", Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")));
                    request.setAttribute("estado", Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                    request.setAttribute("nacionalidad", Lista.getNombreNacionalidad(rs.getString("COD_NACIONALIDAD")));
                    request.setAttribute("ruta", rs.getString("RUTA_ACTA_MAT"));
                    request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
                }
                cn.close();
                st.close();
                rs.close();
                con.cerrarBD();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            request.getRequestDispatcher("/jsp/BAniMatrimonio.jsp").forward(request, response);
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
